<template>
  <div class="app-container">
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-position="right"
      label-suffix=":"
      label-width="120px"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('statusLang.status')" prop="status">
            <el-radio-group v-model="queryParams.interceptStatus">
              <el-radio-button label="null">{{ $t("all") }}</el-radio-button>
              <el-radio-button
                v-for="item in interceptStatusOptions"
                :key="item.key"
                :label="item.value"
              >
                {{ $t("statusLang." + item.title) }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item>
            <template #label>
              <el-select v-model="noType">
                <el-option
                  :value="0"
                  :label="$t('outboundOrderLang.warehouseOutDocNo')"
                />
                <el-option
                  :value="1"
                  :label="$t('outboundOrderLang.rigDocNo')"
                />
                <el-option
                  :value="2"
                  :label="$t('backOrderLang.originalLogisticsTrackingNum')"
                />
                <el-option
                  :value="3"
                  :label="$t('backOrderLang.blendNum')"
                />
              </el-select>
            </template>
            <el-input
              v-model="anyNo"
              :placeholder="$t('backOrderLang.backPleaseEnterTip')"
              type="textarea"
              :rows="3"
              style="width: 350px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depot')" prop="depotId">
            <el-select
              v-model="queryParams.receiptDepotId"
              :placeholder="$t('pleaseSelect')"
              style="width: 200px"
            >
              <el-option
                v-for="item in depotListAllStatusList"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item>
            <template #label>
              <el-select v-model="timeType">
                <el-option :value="0" :label="$t('backOrderLang.createDate')" />
                <el-option :value="1" :label="$t('cutOffOrderLang.cutOffDate')" />
              </el-select>
            </template>
            <el-date-picker
              v-model="timeRange"
              type="daterange"
              value-format="timestamp"
              :start-placeholder="$t('beginDate')"
              :range-separator="$t('to')"
              :end-placeholder="$t('endDate')"
              style="width: 350px"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click="handleAdd"
        >
          {{ $t("add") }}</el-button>
        <!--        <el-button type="primary" size="small" @click="handleExport">-->
        <!--          {{ $t("cutOffOrderLang.export") }}-->
        <!--        </el-button>-->
      </el-col>
    </el-row>
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      class="mt-10"
    >
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        :index="indexAdd"
        align="center"
        width="100"
      />
      <el-table-column
        :label="$t('cutOffOrderLang.orderInfo')"
        prop="outboundNo,providerTrackingNo,channelName"
        align="left"
        min-width="150"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("outboundOrderLang.outDocNo") + ":" }}
            <span>{{ scope.row.outboundNo }}</span>
          </p>
          <p>
            {{ $t("cutOffOrderLang.providerNo") + ":" }}
            <span>{{ scope.row.providerTrackingNo
            }}</span>
          </p>
          <p>
            {{ $t("cutOffOrderLang.logisticsProducts") + ":" }}
            <span>{{ scope.row.channelName }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('cutOffOrderLang.cutOffReason')"
        prop="interceptReason"
        align="center"
        min-width="150"
      />
      <el-table-column
        :label="$t('cutOffOrderLang.cutOffStatus')"
        prop="interceptStatus"
        align="left"
        min-width="150"
      >
        <template slot-scope="scope">
          <p>
            <span>{{ $t("statusLang." + getTitleByValue(interceptStatusOptions, scope.row.interceptStatus)) }}</span>
          </p>
          <!--          <p>-->
          <!--            {{ $t("cutOffOrderLang.remark") + ":" }}-->
          <!--            <span>{{ scope.row.depotCodeAndName }}</span>-->
          <!--          </p>-->
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('cutOffOrderLang.date')"
        prop="createdUserName,gmtCreated,interceptUserName,interceptDate"
        align="left"
        min-width="150"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("cutOffOrderLang.creator") + ":" }}
            <span>{{ scope.row.createdUserName }}</span>
          </p>
          <p>
            {{ $t("cutOffOrderLang.createDate") + ":" }}
            <span>{{ scope.row.gmtCreated
            }}</span>
          </p>
          <p>
            {{ $t("cutOffOrderLang.cutOffUser") + ":" }}
            <span>{{ scope.row.interceptUserName
            }}</span>
          </p>
          <p>
            {{ $t("cutOffOrderLang.cutOffDate") + ":" }}
            <span>{{ scope.row.interceptDate
            }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('operation')"
        width="100"
        align="center"
        fixed="right"
      >
        <template v-if="scope.row.interceptStatus == 1" slot-scope="scope">
          <el-button type="text" @click="handleCancel(scope.row)">
            {{ $t("cancel") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 新增/编辑弹层 -->
    <DepotBaseForm
      v-model="depotBaseParams.id"
      :visible.sync="depotDialog"
      :title="title"
      :operation="depotBaseParams.operation"
      @close="close"
    />
  </div>
</template>

<script>
import DepotBaseForm from './components/info-form.vue'
import { interceptStatusOptionDict } from '@/utils/dictData'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import { getDepotListAllStatus } from '@/api/business/depotManage/depot'
import { cancelIntercept, getInterceptPageList } from '@/api/business/operateManage/intercept'

export default {
  name: 'Depot',
  components: { DepotBaseForm },
  data() {
    return {
      total: 0,
      noType: 0,
      anyNo: null,
      timeType: 0,
      timeRange: [],
      depotListAllStatusList: [],
      loading: false,
      queryParams: {
        interceptStatus: null,
        outboundNo: null,
        receiptDepotId: null,
        pageNo: 1,
        pageSize: 10
      },
      tableData: [], // 列表
      getTitleByValue: getTitleByValue,
      interceptStatusOptions: getDictOptionList(interceptStatusOptionDict), // 状态
      depotBaseParams: {
        id: null,
        operation: 0
      },
      depotDialog: false,
      title: ''
    }
  },
  created() {
    this.getDepotListAllStatus()
    this.getPageList()
  },
  methods: {
    // 重置
    async handleReset() {
      this.queryParams = {
        interceptStatus: null,
        receiptDepotId: null,
        pageNo: 1,
        pageSize: 10
      }
      this.noType = 0
      this.anyNo = null
      this.timeRange = []
      await this.$refs.queryParams.resetFields()
      this.getPageList()
    },
    // 查询
    async handleQuery() {
      this.queryParams.pageNo = 1
      this.getPageList()
    },
    // 获取列表
    async getPageList() {
      this.loading = true
      const anyNo = this.anyNo
      // 退回单号、原物流商单号、原物流单号、混合条件查询
      this.queryParams.outboundNo = undefined
      this.queryParams.platformNo = undefined
      this.queryParams.providerTrackingNo = undefined
      // this.queryParams.blendNum = undefined
      if (this.noType === 0) {
        this.queryParams.outboundNo = anyNo
      }
      if (this.noType === 1) {
        this.queryParams.platformNo = anyNo
      }
      if (this.noType === 2) {
        this.queryParams.providerTrackingNo = anyNo
      }
      if (this.noType === 3) {
        this.queryParams.blendNum = anyNo
      }
      // 时间查询
      const timeRange = this.timeRange || 0
      this.queryParams.createdStart = undefined
      this.queryParams.createdEnd = undefined
      this.queryParams.backRecivedStart = undefined
      this.queryParams.backRecivedEnd = undefined
      if (this.timeType === 0) {
        this.queryParams.gmtCreatedStart = timeRange[0] / 1000
        this.queryParams.gmtCreatedEnd = timeRange[1] / 1000
      }
      if (this.timeType === 1) {
        this.queryParams.interceptDateStart = timeRange[0] / 1000
        this.queryParams.interceptDateEnd = timeRange[1] / 1000
      }
      const res = await getInterceptPageList({ ...this.queryParams })
      if (res.data) {
        this.tableData = res.data.interceptRespList
        this.total = res.data.totalRowNum
      } else {
        this.tableData = []
        this.total = 0
      }
      this.loading = false
    },
    // 所有仓库状态下拉列表
    async getDepotListAllStatus() {
      const { data } = await getDepotListAllStatus()
      this.depotListAllStatusList = data
    },
    // 新增
    async handleAdd() {
      this.depotBaseParams.operation = 1
      this.title = this.$t('add')
      this.depotDialog = true
    },
    // 取消
    async handleCancel(row) {
      const flag = await this.$confirm(
        this.$t('cutOffOrderLang.cancelHint'),
        this.$t('hint'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await cancelIntercept({ id: row.id })
      if (res && res.returnCode === 10000) {
        this.$message({
          type: 'success',
          message: this.$t('handleSuccess')
        })
        this.getPageList()
      }
    },
    // 关闭弹出层
    close() {
      this.depotBaseParams.id = null
      this.depotBaseParams.operation = 0
      this.depotDialog = false
      this.getPageList()
    },
    // 切换页码
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage
      this.getPageList()
    },
    indexAdd(index) {
      // console.log(index)
      const page = this.queryParams.pageNo // 当前页码
      const pagesize = this.queryParams.pageSize // 每页条数
      return index + 1 + (page - 1) * pagesize
    }
  }
}
</script>

<style scss="lang" scoped></style>
